package com.springStudy.boot.redis.firstDemo;

/**
 * @Description
 * @Author ______   __   __   __   ______   __    __
 * /      \ /  | /  | /  | /      \ /  |  /  |
 * $$$$$$  |$$ | $$ | $$ | $$$$$$  |$$ |  $$ |
 * /    $$ |$$ | $$ | $$ | /    $$ |$$ |  $$ |
 * /$$$$$$$ |$$ \_$$ \_$$ |/$$$$$$$ |$$ \__$$ |
 * $$    $$ |$$   $$   $$/ $$    $$ |$$    $$ |
 * $$$$$$$/  $$$$$/$$$$/   $$$$$$$/  $$$$$$$ |
 * /  \__$$ |
 * $$    $$/
 * $$$$$$/
 * @Version V1.0.0
 * @Since 1.0
 * @Date 21.3.26
 */

import ch.qos.logback.classic.pattern.MessageConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.alibaba.fastjson.JSON;
import org.slf4j.helpers.MessageFormatter;

import java.util.stream.Stream;

/**
 * 参数JSON格式化类
 *
 * @author Hollis
 */
public class ArgumentJsonFormatLayout extends MessageConverter {

    @Override
    public String convert(ILoggingEvent event) {
        try {
            return MessageFormatter.arrayFormat(event.getMessage(), Stream.of(event.getArgumentArray())
                    .map(JSON::toJSONString).toArray()).getMessage();
        } catch (Exception e) {
            return event.getMessage();
        }
    }
}
